<?php
require_once ('classes/database.class.php');
$sort = "";
$sortBy = "";
$sortByMode = "";
echo $_POST['sort_mode'];
$db = new database ("localhost", "root", "a0m1b6ml_root", "guestbook");
if (isset($_POST['sort']) && isset($_POST['sort_mode']))
{
	switch ($_POST['sort'])
	{
		case 'sort_user':
			$sortBy = "user_name";
			break;
			case 'sort_mail':
				$sortBy = "email";
				break;
				case 'sort_date':
					$sortBy = "message_date";
					break;
	}
	if ($_POST['sort_mode'] === "asc")
	{
		$sortByMode = "asc";
	}
	else if ($_POST['sort_mode'] === "desc")
	{
		$sortByMode = "desc";
	}
	else {
		$sortByMode = "asc";
	}
}
if ($sortBy !== "" && $sortByMode !== "")
{
	$sort = array ("field" => $sortBy, "mode" => $sortByMode);
}
$xml = $db->getRecords("gb_records", "", "", $sort);
$xsl = new DOMDocument();
$xsl->load('xsl/main.xsl');
$transformer = new XSLTProcessor();
$transformer->importStylesheet($xsl);
if (isset($_GET['status']) && $_GET['status'] != "")
{
	$transformer->setParameter('null', 'status', $_GET['status']);
}
$output = $transformer->transformToXml($xml);
echo $output;
?>